const xml2json = require('pt-core-common').util.xml2json;
const AWS = require('aws-sdk');
AWS.config.update({region:'us-east-2'});

const queueUrl = 'https://sqs.us-east-2.amazonaws.com/599885534814/pt-mws-notification';

const params = {
  QueueUrl: queueUrl, /* required */
  AttributeNames: ['All'],
  VisibilityTimeout: '60',
  MaxNumberOfMessages: '10',
};
let sqsListener = {};
sqsListener.deleteMessage = (receiptHandler) => new Promise((resolve, reject) => {
  const sqs = new AWS.SQS();
  sqs.deleteMessage({QueueUrl: queueUrl, ReceiptHandle: receiptHandler}, function(err, data){
    if (err) {
      reject(err);
      console.error(err, err.stack);
    } // an error occurred
    else {
      resolve(data);
    } // successful response
  });
});

sqsListener.receive = () => new Promise((resolve, reject) => {
  const sqs = new AWS.SQS();
  sqs.receiveMessage(params, function(err, data) {
    if (err) {
      reject(err);
      console.error(err, err.stack);
    } // an error occurred
    else {
      if (data.Messages) {
        // if (data.Messages.length > 1) {
        //   console.error(JSON.stringify(data));
        //   throw Error("there are more than one messages");
        // }
        let messages = [];
        data.Messages.forEach(item => {
          const message_xml = item.Body;
          let message = xml2json(message_xml);
          message["Notification"]["NotificationMetaData"]["MessageId"] = item.MessageId;
          message["Notification"]["NotificationMetaData"]["ReceiptHandle"] = item.ReceiptHandle;
          message["Notification"]["NotificationMetaData"]["SenderId"] = item.Attributes.SenderId;
          message["Notification"]["NotificationMetaData"]["ApproximateFirstReceiveTimestamp"] = item.Attributes.ApproximateFirstReceiveTimestamp;
          message["Notification"]["NotificationMetaData"]["ApproximateReceiveCount"] = item.Attributes.ApproximateReceiveCount;
          message["Notification"]["NotificationMetaData"]["SentTimestamp"] = item.Attributes.SentTimestamp;
          messages.push(message);
        });
        // const message_xml = data.Messages[0].Body;
        // let message = xml2json(message_xml);
        // message["Notification"]["NotificationMetaData"]["MessageId"] = data.Messages[0].MessageId;
        // message["Notification"]["NotificationMetaData"]["ReceiptHandle"] = data.Messages[0].ReceiptHandle;
        // message["Notification"]["NotificationMetaData"]["SenderId"] = data.Messages[0].Attributes.SenderId;
        // message["Notification"]["NotificationMetaData"]["ApproximateFirstReceiveTimestamp"] = data.Messages[0].Attributes.ApproximateFirstReceiveTimestamp;
        // message["Notification"]["NotificationMetaData"]["ApproximateReceiveCount"] = data.Messages[0].Attributes.ApproximateReceiveCount;
        // message["Notification"]["NotificationMetaData"]["SentTimestamp"] = data.Messages[0].Attributes.SentTimestamp;
        resolve(messages);
      } else {
        resolve(null);
      }
    } // successful response
  });
});

module.exports = sqsListener;